package com.carolouvindam;


import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.*;
import com.carolouvindam.Model.Address;
import com.carolouvindam.Model.Client;
import com.carolouvindam.Model.Order;
import com.carolouvindam.database.DataSource;

/**
 * Created by IntelliJ IDEA.
 * User: isen
 * Date: 26/01/12
 * Time: 09:06
 * To change this template use File | Settings | File Templates.
 */
public class OrderSheet extends Activity {

    private long currentIdOrder;
    private Order currentOrder;

    static final int CHANGE_PHONE_DIALOG = 0;
    static final int CHANGE_NAME_DIALOG = 1;
    static final int CHANGE_ADRESS_DIALOG = 2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
        setContentView(R.layout.ordersheet);
        Intent intent = getIntent();


        currentIdOrder = intent.getExtras().getInt("orderIdDB");

        DataSource datasource = new DataSource(this);
        datasource.open();
        this.currentOrder = datasource.getOne((int) this.currentIdOrder);
        datasource.close();
        this.setContentView();


        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new GridImageAdaptater(this, this.currentOrder.getProduits()));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(OrderSheet.this, Application.CAKES[(Integer) v.getTag()].getDescription(), Toast.LENGTH_LONG).show();

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.setContentView();
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        window.setFormat(PixelFormat.RGBA_8888);
    }


    public void cancelChanges(View view) {
        this.dialog.dismiss();
    }

    public void openDialogPhone(View v) {
        showDialog(CHANGE_PHONE_DIALOG);
    }

    public void openDialogAdress(View v) {
        showDialog(CHANGE_ADRESS_DIALOG);
    }

    public void openDialogName(View view) {
        showDialog(CHANGE_NAME_DIALOG);
    }

    public void saveAdresseChange(View view) {

        EditText editText = (EditText) dialog.findViewById(R.id.editabletextAdresse);
        editText.setText(this.currentOrder.getAddress().getLine1());

        EditText editText1 = (EditText) dialog.findViewById(R.id.editabletextAdresse2);
        editText1.setText(this.currentOrder.getAddress().getLine2());

        EditText editText2 = (EditText) dialog.findViewById(R.id.editabletextAdresseCity);
        editText2.setText(this.currentOrder.getAddress().getCity());

        EditText editText3 = (EditText) dialog.findViewById(R.id.editabletextAdresseCity);
        editText3.setText(this.currentOrder.getAddress().getPostalCode());



        Order newOrder = new Order(this.currentOrder.getIdDB(),this.currentOrder.getClient(),new Address(editText.getText().toString(),editText1.getText().toString(),editText2.getText().toString(),editText3.getText().toString()), this.currentOrder.getProduits(),this.currentOrder.getDate()) ;

        this.currentOrder = newOrder;

        this.updateOrderInBDD(this.currentOrder);
        this.dialog.dismiss();
    }

    public void saveNameChanges(View view) {

        EditText editText = (EditText) dialog.findViewById(R.id.editabletextName);
        EditText editText1 = (EditText) dialog.findViewById(R.id.editabletextFirstname);
        Order newOrder = new Order(this.currentOrder.getIdDB(),
                new Client(editText.getText().toString(), editText1.getText().toString(), this.currentOrder.getClient().getPhoneNumber())
                , this.currentOrder.getAddress(), this.currentOrder.getProduits(), this.currentOrder.getDate());
        this.currentOrder = newOrder;

        this.updateOrderInBDD(this.currentOrder);
        this.dialog.dismiss();
    }

    public void savePhoneChanges(View view) {
        EditText editText = (EditText) dialog.findViewById(R.id.editablePhoneNumber);
        Order newOrder = new Order(this.currentOrder.getIdDB(),
                new Client(this.currentOrder.getClient().getName(), this.currentOrder.getClient().getFirstName(), editText.getText().toString())
                , this.currentOrder.getAddress(), this.currentOrder.getProduits(), this.currentOrder.getDate());
        this.currentOrder = newOrder;
        this.updateOrderInBDD(this.currentOrder);
        this.dialog.dismiss();
    }

    private void updateOrderInBDD(Order order) {

        DataSource datasource = new DataSource(this);
        datasource.open();
        datasource.update(order);
        datasource.close();
        this.setContentView();

    }

    private Dialog dialog;

    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case CHANGE_ADRESS_DIALOG:

                dialog = new Dialog(OrderSheet.this);
                dialog.setContentView(R.layout.adressedialog);

                EditText editText0 = (EditText) dialog.findViewById(R.id.editabletextAdresse);
                editText0.setText(this.currentOrder.getAddress().getLine1());

                EditText editText10 = (EditText) dialog.findViewById(R.id.editabletextAdresse2);
                editText10.setText(this.currentOrder.getAddress().getLine2());

                EditText editText20 = (EditText) dialog.findViewById(R.id.editabletextAdresseCity);
                editText20.setText(this.currentOrder.getAddress().getCity());

                EditText editText30 = (EditText) dialog.findViewById(R.id.editabletextAdresseCity);
                editText30.setText(this.currentOrder.getAddress().getPostalCode());

                dialog.setTitle("Address change");
                ((Button) (dialog.findViewById(R.id.button_save_Adresse))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OrderSheet.this.saveAdresseChange(view);
                    }
                });
                ((Button) (dialog.findViewById(R.id.button_save_cancel))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OrderSheet.this.cancelChanges(view);
                    }
                });


                break;
            case CHANGE_NAME_DIALOG:

                dialog = new Dialog(OrderSheet.this);
                dialog.setContentView(R.layout.textdialog);
                EditText editText = (EditText) dialog.findViewById(R.id.editabletextName);
                editText.setText("" + this.currentOrder.getClient().getName());
                EditText editText1 = (EditText) dialog.findViewById(R.id.editabletextFirstname);
                editText1.setText("" + this.currentOrder.getClient().getFirstName());
                dialog.setTitle("Client's changes");
                ((Button) (dialog.findViewById(R.id.button_save_changes))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OrderSheet.this.saveNameChanges(view);
                    }
                });
                ((Button) (dialog.findViewById(R.id.button_save_cancel))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OrderSheet.this.cancelChanges(view);
                    }
                });

                break;
            case CHANGE_PHONE_DIALOG:

                dialog = new Dialog(OrderSheet.this);
                dialog.setContentView(R.layout.phonenumberdialog);
                EditText editText2 = (EditText) dialog.findViewById(R.id.editablePhoneNumber);
                editText2.setText(this.currentOrder.getClient().getPhoneNumber());
                dialog.setTitle("Client's Phone Number change");
                ((Button) (dialog.findViewById(R.id.button_save_Phone_changes))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OrderSheet.this.savePhoneChanges(view);
                    }
                });
                ((Button) (dialog.findViewById(R.id.button_save_Phone_cancel))).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OrderSheet.this.cancelChanges(view);
                    }
                });
                break;

            default:
                dialog = null;
        }
        return dialog;
    }

    private void setContentView() {

        TextView clientName = (TextView) findViewById(R.id.OrderSheetName);
        clientName.setText(this.currentOrder.getClient().getFirstName() + " " + this.currentOrder.getClient().getName());

        TextView address = (TextView) findViewById(R.id.OrderSheetAddress);
        address.setText(this.currentOrder.getAddress().toString());

        TextView number = (TextView) findViewById(R.id.OrderSheetNumber);
        number.setText(this.currentOrder.getClient().getPhoneNumber());

    }
}
